import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/gradient_line_chart_painter.dart';
import 'package:x3_charts_core/model/gradient_line_chart_model.dart';

///**
///* Description: 渐变折线图
///*/
class GradientLineChartWidget extends StatefulWidget {
  final List<GradientLineChartModel> chartList;

  ///四周边距
  final EdgeInsets padding;

  ///是否绘制Y轴
  final bool isDrawYAxis;

  ///是否绘制X轴
  final bool isDrawXAxis;

  ///轴线颜色
  final Color axisColor;

  ///Y轴标题
  final String yAxisTitle;

  ///渐变颜色
  final List<Color> linearColor;

  ///渐变系数
  final List<double> linearColorStops;

  ///是否需要扩展(自动添加一个起点【x == 0】和终点【x == maxWidth】)
  final bool needToExpand;

  ///是否描边（绘制一根折线覆盖顶部）
  final bool isStroke;

  ///描边颜色
  final Color strokeColor;

  /// 文本Style
  final TextStyle textStyle;

  GradientLineChartWidget({
    @required this.chartList,
    this.padding = const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0, bottom: 20),
    this.isDrawYAxis = false,
    this.isDrawXAxis = true,
    this.axisColor = const Color(0xFFF3F3F3),
    this.linearColor = const [Color(0xFFDA9CB2), Colors.white],
    this.linearColorStops = const [0.5, 1.0],
    this.yAxisTitle = "",
    this.needToExpand = true,
    this.isStroke = false,
    this.strokeColor = const Color(0xFFF04134),
    this.textStyle = const TextStyle(color: Color(0xFF333333), fontSize: 10),
  });

  @override
  _GradientLineChartWidgetState createState() => _GradientLineChartWidgetState();
}

class _GradientLineChartWidgetState extends State<GradientLineChartWidget> {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return Container(
          child: CustomPaint(
            size: Size(constraints.maxWidth, constraints.maxHeight),
            painter: GradientLineChartPainter(
              chartList: widget.chartList,
              padding: widget.padding,
              isDrawXAxis: widget.isDrawXAxis,
              isDrawYAxis: widget.isDrawYAxis,
              axisColor: widget.axisColor,
              yAxisTitle: widget.yAxisTitle,
              linearColor: widget.linearColor,
              linearColorStops: widget.linearColorStops,
              needToExpand: widget.needToExpand,
              isStroke: widget.isStroke,
              strokeColor: widget.strokeColor,
              textStyle: widget.textStyle,
            ),
          ),
        );
      },
    );
  }
}
